﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using Microsoft.Win32;
using JMT.Data;

namespace JMT.Shell
{
    public static class ShellCommand
    {
        private static MainWindow _mainWindow;

        public static RoutedUICommand NewCommand { get; set; }
        public static RoutedUICommand SaveCommand { get; set; }
        public static RoutedUICommand OpenCommand { get; set; }

        static ShellCommand()
        {
        }

        public static void Register(MainWindow window)
        {
            _mainWindow = window;

            NewCommand = new RoutedUICommand();
            NewCommand.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control, "Ctrl+N"));
            window.Resources.Add("jnew", NewCommand);
            CommandBinding newBinding = new CommandBinding(NewCommand, new ExecutedRoutedEventHandler(OnNewExecuted));
            window.CommandBindings.Add(newBinding);

            SaveCommand = new RoutedUICommand();
            SaveCommand.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control, "Ctrl+S"));
            window.Resources.Add("jsave", SaveCommand);
            CommandBinding saveBinding = new CommandBinding(SaveCommand, new ExecutedRoutedEventHandler(OnSaveExecuted));
            window.CommandBindings.Add(saveBinding);

            OpenCommand = new RoutedUICommand();
            OpenCommand.InputGestures.Add(new KeyGesture(Key.O, ModifierKeys.Control, "Ctrl+O"));
            window.Resources.Add("jopen", OpenCommand);
            CommandBinding openBinding = new CommandBinding(OpenCommand, new ExecutedRoutedEventHandler(OnOpenExecuted));
            window.CommandBindings.Add(openBinding);
        }

        private static void OnNewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            _mainWindow._jSolution = new Data.JSolution();
            _mainWindow._jSolution.Id = Guid.NewGuid();
            _mainWindow._jSolution.Name = "NewSolution";

            Data.JClass jClass = new Data.JClass();
            jClass.Id = Guid.NewGuid();
            jClass.Name = "NewClass";//_mainWindow._jSolution.ClassCollection.GetUniqueName();
            _mainWindow._jSolution.Add(jClass);

            Data.JDiagram jDiagram = new Data.JDiagram();
            jDiagram.Id = Guid.NewGuid();
            jDiagram.Name = "NewDiagram";// _mainWindow._jSolution.DiagramCollection.GetUniqueName();
            _mainWindow._jSolution.Add(jDiagram);
            _mainWindow._ctlDiagramContainer.Clear();
            _mainWindow._ctlDiagramContainer.Show(_mainWindow._jSolution, jDiagram);

            _mainWindow._ctlTreeView.Bind(_mainWindow._jSolution);
            JData.Current.Solution = _mainWindow._jSolution;
        }

        private static void OnSaveExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (_mainWindow._store == null)
            {
                _mainWindow._store = new Data.JMSolutionStore();
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = _mainWindow._jSolution.Name;
                dlg.Filter = "*.xml|*.xml";
                if (dlg.ShowDialog() == true)
                {
                    //new Data.JMSolutionStore().Save(_solution, @"c:\jmt\");
                    _mainWindow._store.SaveAs(_mainWindow._jSolution, dlg.FileName);
                    MessageBox.Show("保存成功!");
                }
            }
            else
            {
                _mainWindow._store.Save(_mainWindow._jSolution);
            }
        }

        private static void OnOpenExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == true)
            {
                _mainWindow._store = new Data.JMSolutionStore();
                _mainWindow._jSolution = _mainWindow._store.Load(dlg.FileName);
                _mainWindow._ctlDiagramContainer.Clear();

                //_ctlGrid.Children.Remove(_diagram);
                //_diagram = new Controls.ClassDiagram(_solution, _solution.DiagramCollection.FirstOrDefault());
                //_ctlGrid.Children.Add(_diagram);
                //Grid.SetColumn(_diagram, 1);

                _mainWindow._ctlTreeView.Bind(_mainWindow._jSolution);
                JData.Current.Solution = _mainWindow._jSolution;
            }
        }
    }
}
